package com.uphone.kingmall.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.SizeUtils;
import com.china.zhuoyue.R;
import com.uphone.kingmall.bean.GoodsListBean;
import com.uphone.kingmall.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tzh on 2020/6/15.
 */
public class ShopAllGoodsAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private List<GoodsListBean.DataBean> dataList;
    private Context mContext;
    private AddGoodsListener mListener;

    public ShopAllGoodsAdapter(Context context) {
        mContext = context;
        inflater = LayoutInflater.from(context);
        dataList = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public GoodsListBean.DataBean getItem(int position) {
        if (dataList.size() == 0 || dataList.size() < position) {
            return null;
        }
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        HolderView holderView;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.kingmall_item_goodslist_liner_add, parent, false);
            holderView = new HolderView(convertView);
            convertView.setTag(holderView);
        } else {
            holderView = (HolderView) convertView.getTag();
        }
        if (position < dataList.size()) {
            final GoodsListBean.DataBean bean = dataList.get(position);
            GlideUtils.getInstance().loadCornerImage(mContext, bean.getGoodsMainImg(),
                    SizeUtils.dp2px(2), holderView.iv_icon);
            holderView.tv_name.setText(bean.getGoodsName());
            holderView.tv_price.setText(String.format("¥%s", bean.getGoodsPrice()));
            holderView.tv_yue_xiao.setText(String.format("月销%s", bean.getGoodsSaleNum()));
            if (bean.shopCarGoodsNum > 0) {
                holderView.tv_goods_count.setText(String.valueOf(bean.shopCarGoodsNum));
                holderView.tv_goods_count.setVisibility(View.VISIBLE);
            } else {
                holderView.tv_goods_count.setVisibility(View.GONE);
            }
            //添加到购物车
            holderView.v_add_to_car.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) mListener.onAddClick(bean);
                }
            });
        }
        return convertView;
    }

    public void clearData() {
        if (dataList != null)
            dataList.clear();
    }

    private final static class HolderView {
        ImageView iv_icon;
        TextView tv_goods_count, tv_name, tv_price, tv_yue_xiao;
        View v_add_to_car;

        HolderView(View view) {
            iv_icon = view.findViewById(R.id.iv_icon);
            tv_goods_count = view.findViewById(R.id.tv_goods_count);
            tv_name = view.findViewById(R.id.tv_name);
            tv_price = view.findViewById(R.id.tv_price);
            tv_yue_xiao = view.findViewById(R.id.tv_yue_xiao);
            v_add_to_car = view.findViewById(R.id.v_add_to_car);
        }
    }

    public void setData(List<GoodsListBean.DataBean> dataList) {
        if (dataList == null || dataList.size() == 0) return;
        this.dataList = dataList;
    }

    public interface AddGoodsListener {
        void onAddClick(GoodsListBean.DataBean bean);
    }

    public void setAddGoodsListener(AddGoodsListener mListener) {
        this.mListener = mListener;
    }
}
